package service;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import model.Farmacia;

public class FarmaciaService {
	
	protected EntityManager entityManager;
	
	public FarmaciaService(EntityManager entityManager) {
		this.entityManager = entityManager;
	}
	
	public Farmacia criaFarmacia(String endereco, String nome, String telefone) {
		Farmacia farmacia = new Farmacia();
		farmacia.setEndereco(endereco);
		farmacia.setNome(nome);
		farmacia.setTelefone(telefone);
		
		insereFarmacia(farmacia);
		return farmacia;
	}
	
	public Farmacia findFarmacia(Long id) {
		Farmacia Farmacia = entityManager.getReference(Farmacia.class, id);
		return Farmacia;
	}
	
	public List<Farmacia> listaFarmacias() {
		TypedQuery<Farmacia> query = entityManager.createQuery(
				"SELECT e FROM Farmacia e", Farmacia.class);
		return query.getResultList();
	}
	
	public void deletaFarmacias() {
		TypedQuery<Farmacia> query = entityManager.createQuery(
				"DELETE FROM Farmacia", Farmacia.class);
		query.executeUpdate();
	}
	
	public void insereFarmacia(Farmacia emp) {
		entityManager.persist(emp);
	}
	
	public void removeFarmacia(Long id) {
		Farmacia farma = findFarmacia(id);
		if (farma != null) {
			entityManager.remove(farma);
		}
	}


}
